var imgText = "";
var errorEmail = 0;
var errorPhone = 0;
var errorUsername = 0;
var errorNickname = 0;
var errorPassword = 0;
var errorRepassWord = 0;
var errorSex = 0;
function submit(){
	var img = imgText;
	var userName = $("#userName").val();
	var nickName = $("#nickName").val();
	var password = $("#password").val();
	var email = $("#email").val();
	var phone = $("#phone").val();
	var sexs = $("input[name='sex']");
	var sex ="";
	Array.from(sexs).filter(item=>item.checked).forEach((item)=>{
		sex=item.value;
	});
	if(userName === null || userName === ""){
    	layer.msg("请输入用户名！", {icon: 5});
    	return false;
	}
	if(nickName === null || nickName === ""){
    	layer.msg("请输入昵称！", {icon: 5});
        return false;
	}
	if(sex === null || sex === ""){
    	layer.msg("请选择性别！", {icon: 5});
        return false;
	}
	if(phone === null || phone === ""){
    	layer.msg("请输入手机号！", {icon: 5});
        return false;
	}
	if(email === null || email === ""){
    	layer.msg("请输入邮箱！", {icon: 5});
        return false;
	}
	if(password === null || password === ""){
    	layer.msg("请输入密码！", {icon: 5});
        return false;
	}
	if(img === null || img === ""){
    	layer.msg("请选择头像上传！", {icon: 5});
        return false;
	}
	if(errorRepassWord == 0 && errorPassword == 0 && errorNickname == 0 && errorUsername == 0 && errorEmail == 0 && errorPhone == 0 && errorSex == 0 ){
        $.ajax({
            type : "post",
            url : "/TeaCulture/register/do",
            data : { "userName" : userName , "password" : password, "nickName" : nickName, "email" : email, "phone" : phone, "sex" : sex,"img":img},
            dataType : "json",
            traditional : true,
            success : function(data){
                if(data.result.code == 1){
                    window.location.href="login";
                }else{
                    layer.msg(data.result.des, {icon: 5});
                }
            },
            error : function(data){
                layer.msg(data.result.des, {icon: 5});
            }
        })
    }else{
        layer.msg("请按提示修改完成在提交！", {icon: 5});
        return false;
    }

}
function init(){
    //email检验置空
    document.getElementById("email").addEventListener("focus",()=>{
        document.querySelector(".email_error_msg").innerHTML="";
        document.querySelector(".email_error_img").classList.remove("errorImg");
        document.querySelector(".email_error_msg").classList.remove("errorMessage");
        document.querySelector(".email_error_msg").classList.remove("okMessage");
        document.querySelector(".email_error_img").classList.remove("okImg");
    },false);
    //email检验
    document.getElementById("email").addEventListener("blur",()=>{
        let regExp=/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
        if(!regExp.test(document.getElementById("email").value)){
            document.querySelector(".email_error_msg").innerHTML="邮箱格式不正确！";
            document.querySelector(".email_error_msg").classList.add("errorMessage");
            document.querySelector(".email_error_img").classList.add("errorImg");
            errorEmail = 1;
        }else{
            document.querySelector(".email_error_msg").innerHTML="邮箱格式符合要求！";
            document.querySelector(".email_error_msg").classList.add("okMessage");
            document.querySelector(".email_error_img").classList.add("okImg");
            errorEmail = 0;
        }
    },false);
     //phone检验置空
     document.getElementById("phone").addEventListener("focus",()=>{
        document.querySelector(".phone_error_msg").innerHTML="";
        document.querySelector(".phone_error_img").classList.remove("errorImg");
        document.querySelector(".phone_error_msg").classList.remove("errorMessage");
        document.querySelector(".phone_error_msg").classList.remove("okMessage");
        document.querySelector(".phone_error_img").classList.remove("okImg");
    },false);
    //phone检验
    document.getElementById("phone").addEventListener("blur",()=>{
        let regExpp=/^(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$/;
        if(!regExpp.test(document.getElementById("phone").value)){
            document.querySelector(".phone_error_msg").innerHTML="手机号格式不正确！";
            document.querySelector(".phone_error_msg").classList.add("errorMessage");
            document.querySelector(".phone_error_img").classList.add("errorImg");
            errorPhone = 1;
        }else{
            document.querySelector(".phone_error_msg").innerHTML="手机号格式符合要求！";
            document.querySelector(".phone_error_msg").classList.add("okMessage");
            document.querySelector(".phone_error_img").classList.add("okImg");
            errorPhone = 0;
        }
    },false);
    //用户名检验置空
    document.getElementById("userName").addEventListener("focus",()=>{
        document.querySelector(".userName_error_msg").innerHTML="";
        document.querySelector(".userName_error_img").classList.remove("errorImg");
        document.querySelector(".userName_error_msg").classList.remove("errorMessage");
        document.querySelector(".userName_error_msg").classList.remove("okMessage");
        document.querySelector(".userName_error_img").classList.remove("okImg");
    },false);
    //用户名检验
    document.getElementById("userName").addEventListener("blur",()=>{
        let r1=/^[a-zA-Z0-9_]*$/;
        let len=document.getElementById("userName").value.length;
        if(len==0){
            document.querySelector(".userName_error_msg").innerHTML="用户名不为空！";
            document.querySelector(".userName_error_msg").classList.add("errorMessage");
            document.querySelector(".userName_error_img").classList.add("errorImg");
            errorUsername = 1;
        }else if(!r1.test(document.getElementById("userName").value)){
            document.querySelector(".userName_error_msg").innerHTML="用户名只能包含数字字母下划线！";
            document.querySelector(".userName_error_msg").classList.add("errorMessage");
            document.querySelector(".userName_error_img").classList.add("errorImg");
            errorUsername = 1;
        }else{
            document.querySelector(".userName_error_msg").innerHTML="用户名格式符合要求！";
            document.querySelector(".userName_error_msg").classList.add("okMessage");
            document.querySelector(".userName_error_img").classList.add("okImg");
            errorUsername = 0;
        }
        var username = $("#userName").val();
    	$.ajax({
	    	 type : "post",
	         url : "/TeaCulture/register/check",
	         data : { "userName" : username},
	         dataType : "json",
	         traditional : true,
	         success : function(result){
	        	 if(result.result.code === "1"){
	        		if(result.counts === "1"){
	        			document.querySelector(".userName_error_msg").innerHTML="";
	        	        document.querySelector(".userName_error_img").classList.remove("errorImg");
	        	        document.querySelector(".userName_error_msg").classList.remove("errorMessage");
	        	        document.querySelector(".userName_error_msg").classList.remove("okMessage");
	        	        document.querySelector(".userName_error_img").classList.remove("okImg");
	        	        document.querySelector(".userName_error_msg").innerHTML="用户名已存在！";
	                    document.querySelector(".userName_error_msg").classList.add("errorMessage");
	                    document.querySelector(".userName_error_img").classList.add("errorImg");
	                    errorUsername = 1;
	        		}
		        }else{
		        	alert(result.result.des);
		        }     	
	        },    
	        error : function(result){
	        	alert(result.result.des);
	        }
	    });
    	
    },false);
    //昵称检验置空
    document.getElementById("nickName").addEventListener("focus",()=>{
        document.querySelector(".nickName_error_msg").innerHTML="";
        document.querySelector(".nickName_error_img").classList.remove("errorImg");
        document.querySelector(".nickName_error_msg").classList.remove("errorMessage");
        document.querySelector(".nickName_error_msg").classList.remove("okMessage");
        document.querySelector(".nickName_error_img").classList.remove("okImg");
    },false);
    //昵称检验
    document.getElementById("nickName").addEventListener("blur",()=>{
        let len2=document.getElementById("nickName").value.length;
        if(len2==0){
            document.querySelector(".nickName_error_msg").innerHTML="昵称不为空！";
            document.querySelector(".nickName_error_msg").classList.add("errorMessage");
            document.querySelector(".nickName_error_img").classList.add("errorImg");
            errorNickname = 1;
        }else{
            document.querySelector(".nickName_error_msg").innerHTML="昵称格式符合要求！";
            document.querySelector(".nickName_error_msg").classList.add("okMessage");
            document.querySelector(".nickName_error_img").classList.add("okImg");
            errorNickname = 0;
        }
    },false);
    //密码检验置空
    document.getElementById("password").addEventListener("focus",()=>{
        document.querySelector(".password_error_msg").innerHTML="";
        document.querySelector(".password_error_img").classList.remove("errorImg");
        document.querySelector(".password_error_msg").classList.remove("errorMessage");
        document.querySelector(".password_error_msg").classList.remove("okMessage");
        document.querySelector(".password_error_img").classList.remove("okImg");
    },false);
    //密码检验
    document.getElementById("password").addEventListener("blur",()=>{
        let len2=document.getElementById("password").value.length;
        if(len2==0){
            document.querySelector(".password_error_msg").innerHTML="密码不为空！";
            document.querySelector(".password_error_msg").classList.add("errorMessage");
            document.querySelector(".password_error_img").classList.add("errorImg");
            errorPassword = 1;
        }else if(len2<8||len2>20){
            document.querySelector(".password_error_msg").innerHTML="密码长度不能小于8，不能大于20！";
            document.querySelector(".password_error_msg").classList.add("errorMessage");
            document.querySelector(".password_error_img").classList.add("errorImg");
            errorPassword = 1;
        }
        else{
            document.querySelector(".password_error_msg").innerHTML="密码格式符合要求！";
            document.querySelector(".password_error_msg").classList.add("okMessage");
            document.querySelector(".password_error_img").classList.add("okImg");
            errorPassword = 0;
        }
    },false);
    //再输入一次检验置空
    document.getElementById("rePassword").addEventListener("focus",()=>{
        document.querySelector(".rePassword_error_msg").innerHTML="";
        document.querySelector(".rePassword_error_img").classList.remove("errorImg");
        document.querySelector(".rePassword_error_msg").classList.remove("errorMessage");
        document.querySelector(".rePassword_error_msg").classList.remove("okMessage");
        document.querySelector(".rePassword_error_img").classList.remove("okImg");
    },false);
    //再输入一次检验
    document.getElementById("rePassword").addEventListener("blur",()=>{
        let len3=document.getElementById("rePassword").value.length;
        let value2=document.getElementById("password").value;
        let value3=document.getElementById("rePassword").value;
        if(len3==0){
            document.querySelector(".rePassword_error_msg").innerHTML="输入不为空！";
            document.querySelector(".rePassword_error_msg").classList.add("errorMessage");
            document.querySelector(".rePassword_error_img").classList.add("errorImg");
            errorRepassWord = 1;
        }else if(value3!=value2){
            document.querySelector(".rePassword_error_msg").innerHTML="输入必须与前一个输入密码一致！";
            document.querySelector(".rePassword_error_msg").classList.add("errorMessage");
            document.querySelector(".rePassword_error_img").classList.add("errorImg");
            errorRepassWord = 1;
        }else{
            document.querySelector(".rePassword_error_msg").innerHTML="输入符合要求！";
            document.querySelector(".rePassword_error_msg").classList.add("okMessage");
            document.querySelector(".rePassword_error_img").classList.add("okImg");
            errorRepassWord = 0;
        }
    },false);
}
window.addEventListener("load",init,false);
$(function(){ 
	$("#register").on('click',function(){
		submit();
	})
	$(document).keydown(function(event){ 
		if(event.keyCode==13){ 
			submit();
		} 
	}); 
	 // 头像预览
    $("#avatar_file").change(function () {
        // 获取上传文件对象
        var file = $(this)[0].files[0];
        // 读取文件URL
        var reader = new FileReader();
        reader.readAsDataURL(file);
        // 阅读文件完成后触发的事件
        reader.onload = function (event) {
            // 读取的URL结果：this.result
            $("#avatar_img").attr("src", this.result);
            var txt = event.target.result;
            var img = document.createElement("img");
            img.src = txt;//将图片base64字符串赋值给img的src
            imgText = txt;
            console.log(img.src);
        }
    });


});

